package com.it.pojo;

public class EquationContainer {
    private Node[] arr = new Node[10];

    private int modCount;

    public EquationContainer() {
        this.modCount=0;
    }

    public int getSize(){
        return this.modCount;
    }

    public boolean add(Equation equation) {
        int index = equation.getResult() % 10;
        if(index<0){
            index=Math.abs(index);
        }
        Node head = arr[index];
        if (arr[index] == null) {
            arr[index] = new Node(equation);
            this.modCount++;
            return true;
        } else if (arr[index].getEquation().equals(equation)) {
            return false;
        } else {
            while (head.getNext() != null) {
                if (equation.equals(head.getNext().getEquation())) {
                    return false;
                }
                head=head.getNext();

            }
            head.setNext(new Node(equation));
            this.modCount++;
            return true;
        }

    }

    public Equation[] getEquationArray(){
        Equation[] equations=new Equation[modCount];
        int index=0;
        for (int i = 0; i < 10; i++) {
            if (arr[i] == null) {
                continue;
            }
            equations[index++]=(arr[i].getEquation());
            Node temp=arr[i];
            while(temp.getNext()!=null){
                equations[index++]=(temp.getNext().getEquation());
                temp=temp.getNext();
            }
        }
        return equations;


    }



    class Node {
        private Equation equation;

        private Node next;

        public Node() {
        }

        public Node(Equation equation) {
            this.equation = equation;
        }

        public Equation getEquation() {
            return equation;
        }

        public void setEquation(Equation equation) {
            this.equation = equation;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }

}